package com.aitoedu.server.handler;

import cn.dev33.satoken.util.SaResult;
import com.aitoedu.server.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@ControllerAdvice
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public SaResult e(Exception e){
        log.error("未知异常：{}",e.getMessage());
        return SaResult.error(e.getMessage());
    }

    /**
     * 捕获业务异常
     * @param
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public SaResult exceptionHandler(BaseException e){
        log.error("异常信息：{}",e.getMessage());
        return SaResult.error(e.getMessage());
    }

}
